package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.random.IceRandomUtils;
import cc.edt.iceutils2.string.IceStringUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j

public class SeletFirstMoneyServiceImpl implements SelectFirstMoneyService {

    @Resource
    private EdtApiService edtApiService;

    @Resource
    private OrderCreditCKService orderCreditCKService;


    @Resource
    private OrderCreditKdService orderCreditKdService;

    @Resource
    private OrderChannelService orderChannelService;
    @Resource
    private CommissionRootRuleService commissionRootRuleService;

    @Override
    public ActionResult selectFirstMoneyCk(OrderChannel orderChannel) {
        ActionResult result = new ActionResult();
        result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
        OrderCreditCK orderCreditCK = orderCreditCKService.selectOrderCreditCkByOrderCode(orderChannel.getOrderCode());
        if(orderCreditCK == null) {
            orderCreditCK = new OrderCreditCK();
            orderCreditCK.setId(IceRandomUtils.longUUID());
            BeanUtils.copyProperties(orderChannel,orderCreditCK,"id");
            //查询首充
            FirstmoneyLog firstmoneyLog = edtApiService.selectInterfaceA(orderChannel);
            if(firstmoneyLog.getUniBssInfo() != null){
                switch (IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getRSP_CODE(),"-998")){
                    case CommonConstant.RSP_CODE_SUCCESS:
                        if(IceStringUtils.isNotBlank(firstmoneyLog.getDifUserId())){
                            setFlowOver(orderCreditCK,"查询首充成功但是userid有变化");
                            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                        }else if(!IceStringUtils.blankToDefault(orderChannel.getDevelopStaffId(),"-999").equals(IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getDEVELOP_STAFF_ID(),"-999"))){
                            setFlowOver(orderCreditCK,"查询首充成功但是DEVELOP_STAFF_ID 与订单不同");
                            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                        }else{
                            if(firstmoneyLog.getFirstMoney() > 0){
                                setFlowNotOver(orderCreditCK);
                                result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                            }
                        }
                        break;
//                    case CommonConstant.RSP_CODE_0002:
//                        setFlowOver(orderCreditCK,IceJsonStringUtils.toJsonString(firstmoneyLog.getUniBssInfo()));
//                        result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
//                        break;
                    default:

                }

                setFirstMoney(orderCreditCK,firstmoneyLog);
            }
        }else{
            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
        }
        result.setResultList(orderCreditCK);
        return result;

    }

    private void setFlowOver(OrderCreditCK orderCreditCK,String remark){
        orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
        orderCreditCK.setRemark(remark);
    }
    private void setFlowNotOver(OrderCreditCK orderCreditCK){
        orderCreditCK.setOverState(CommonConstant.FLOW_NOT_OVER);
        orderCreditCK.setRemark("查询成功可以计算佣金");
    }

    private void setFlowOver(OrderCreditKd orderCreditKd,String remark){
        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
        orderCreditKd.setRemark(remark);
    }
    private void setFlowNotOver(OrderCreditKd orderCreditKd){
        orderCreditKd.setOverState(CommonConstant.FLOW_NOT_OVER);
        orderCreditKd.setRemark("查询成功可以计算佣金");
    }

    private void setFirstMoney(OrderCreditCK orderCreditCK,FirstmoneyLog firstmoneyLog){
        orderCreditCK.setFirstMoney(firstmoneyLog.getFirstMoney());
        orderCreditCK.setFirstDate(firstmoneyLog.getFirstDate());
        orderCreditCK.setUserId(firstmoneyLog.getUserId());
    }
    @Override
    public ActionResult selectFirstMoneyKd(OrderChannel orderChannel) {
        //查询ordercreditkd中数据
        ActionResult result = new ActionResult();
        result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
        OrderCreditKd orderCreditKd = orderCreditKdService.selectOrderCreditKdByOrderCode(orderChannel.getOrderCode());
        if(orderCreditKd == null){
            orderCreditKd = new OrderCreditKd();
            orderCreditKd.setId(IceRandomUtils.longUUID());
            BeanUtils.copyProperties(orderChannel, orderCreditKd, "id");
            orderCreditKd.setTelNumber(orderChannel.getServiceCode());
            //查询接口a
            FirstmoneyLog firstmoneyLog = edtApiService.selectInterfaceA(orderChannel);
            //如果查询成功
            if(firstmoneyLog.getUniBssInfo() != null){

                setFirstMoney(orderCreditKd,firstmoneyLog);
                switch (IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getRSP_CODE(),"-998")){
                    case CommonConstant.RSP_CODE_SUCCESS: case CommonConstant.RSP_CODE_0001:
                        if(IceStringUtils.isNotBlank(firstmoneyLog.getDifUserId())){
                            setFlowOver(orderCreditKd,"查询首充成功但是USER_ID 与订单不同");
                            setSingleProduct(orderCreditKd);
                            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                        }else if(!IceStringUtils.blankToDefault(orderChannel.getDevelopStaffId(),"-999").equals(IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getDEVELOP_STAFF_ID(),"-999"))){
                            setFlowOver(orderCreditKd,"查询首充成功但是DEVELOP_STAFF_ID 与订单不同");
                            setSingleProduct(orderCreditKd);
                            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                        }else{
                            //验证规则，
                            if(commissionRootRuleService.matchCommisionRule(orderCreditKd)){
                                // 匹配到了，返回，类型为单产品
                                setSingleFlowNoOver(orderCreditKd);
                                result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                            }else{
                                //没有匹配到规则
                                //查询b接口
                                edtApiService.selectInterfaceB(orderCreditKd);
                                setRelationProduct(orderCreditKd);
                                if (IceStringUtils.isNotBlank(orderCreditKd.getPhoneNumber())) {
                                    result.setSuccess(selectFirstWithPhone(orderCreditKd,IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getRSP_CODE(),"-998")));
                                }else{
                                    log.info("查询B接口没有解析出手机号码");
                                }
                            }
                        }
                        break;
//                    case CommonConstant.RSP_CODE_0002:
//                        setFlowOver(orderCreditKd,IceJsonStringUtils.toJsonString(firstmoneyLog.getUniBssInfo()));
//                        result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
//                        break;
                    default:
                }

            }
        }else{
            //如果存在,则直接返回
            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
        }
        result.setResultList(orderCreditKd);
        return result;

    }

    @Override
    public ActionResult selectFirstMoneyCkC(OrderChannel orderChannel) {
        return selectFirstMoneyCk(orderChannel);
    }

    @Override
    public ActionResult selectFirstMoneyKdC(OrderChannel orderChannel) {
        //查询ordercreditkd中数据
        ActionResult result = new ActionResult();
        result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
        OrderCreditKd orderCreditKd = orderCreditKdService.selectOrderCreditKdByOrderCode(orderChannel.getOrderCode());
        if(orderCreditKd == null){
            orderCreditKd = new OrderCreditKd();
            orderCreditKd.setId(IceRandomUtils.longUUID());
            BeanUtils.copyProperties(orderChannel, orderCreditKd, "id");
            orderCreditKd.setTelNumber(orderChannel.getServiceCode());
            //查询接口a
            FirstmoneyLog firstmoneyLog = edtApiService.selectInterfaceA(orderChannel);
            //如果查询成功
            if(firstmoneyLog.getUniBssInfo() != null){

                setFirstMoney(orderCreditKd,firstmoneyLog);
                switch (IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getRSP_CODE(),"-998")){
                    case CommonConstant.RSP_CODE_SUCCESS: case CommonConstant.RSP_CODE_0001:
                        if(IceStringUtils.isNotBlank(firstmoneyLog.getDifUserId())){
                            setFlowOver(orderCreditKd,"查询首充成功但是USER_ID 与订单不同");
                            setSingleProduct(orderCreditKd);
                            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                        }else if(!IceStringUtils.blankToDefault(orderChannel.getDevelopStaffId(),"-999").equals(IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getDEVELOP_STAFF_ID(),"-999"))){
                            setFlowOver(orderCreditKd,"查询首充成功但是DEVELOP_STAFF_ID 与订单不同");
                            setSingleProduct(orderCreditKd);
                            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                        }else{
                            //验证规则，
                            if(commissionRootRuleService.matchCommisionRuleC(orderCreditKd)){
                                // 匹配到了，返回，类型为单产品
                                setSingleFlowNoOver(orderCreditKd);
                                result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
                            }else{
                                //没有匹配到规则
                                //查询b接口
                                edtApiService.selectInterfaceB(orderCreditKd);
                                setRelationProduct(orderCreditKd);
                                if (IceStringUtils.isNotBlank(orderCreditKd.getPhoneNumber())) {
                                    result.setSuccess(selectFirstWithPhoneC(orderCreditKd,IceStringUtils.blankToDefault(firstmoneyLog.getUniBssInfo().getRSP_CODE(),"-998")));
                                }else{
                                    log.info("查询B接口没有解析出手机号码");
                                }
                            }
                        }
                        break;
                    default:
                }

            }
        }else{
            //如果存在,则直接返回
            result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
        }
        result.setResultList(orderCreditKd);
        return result;

    }

    private void setSingleFlowNoOver(OrderCreditKd orderCreditKd){
        setFlowNotOver(orderCreditKd);
        setSingleProduct(orderCreditKd);
    }

    private boolean selectFirstWithPhone(OrderCreditKd orderCreditKd,String type){
        //判断手机号是否为本平台
        OrderChannel order = orderChannelService.selectOrderChannelByNumberPhone(orderCreditKd.getPhoneNumber());
        if (order == null) {
            //TODO 后加如果 0001 手机号没匹配规则，则需要重新查询接口，即发挥false 有时间优化
            if(CommonConstant.RSP_CODE_0001.equals(type)){
                return false;
            }else{
                //如果不是流程结束
                setFlowOver(orderCreditKd,"手机号码不在本平台");
                return true;
            }

        } else {
            //如果是 查询手机号和useid在平台是否结算过宽带订单
            OrderCreditKd orderCreditKdUsed = orderCreditKdService.selectOrderCriditKdByPhoneAndUserId(orderCreditKd.getPhoneNumber(), orderCreditKd.getUserId());
            if (orderCreditKdUsed != null) {
                //如果有流程结束
                setFlowOver(orderCreditKd,"手机号和userid在本平台计算过佣金");
                return true;
            } else {
                //如果没有 用手机号查询A 接口
                ActionResult selectCkResult = selectFirstMoneyCk(order);
                setFirstMoney(orderCreditKd, (OrderCreditCK) selectCkResult.getResultList());
                if(orderCreditKd.getFirstMoney() != null && orderCreditKd.getFirstMoney() >0){
                    //TODO 后加如果 0001 手机号没匹配规则，则需要重新查询接口，即发挥false 有时间优化
                    if(CommonConstant.RSP_CODE_0001.equals(type)){
                        if(orderCreditKd.getPhoneFirstMoney() == null){
                            return false;
                        }else{
                            if(!commissionRootRuleService.matchCommisionRuleKd(orderCreditKd)){
                                return false;
                            }
                        }
                    }
                    setFlowNotOver(orderCreditKd);
                    return true;
                }else{
                    return false;
                }
            }
        }
    }


    private boolean selectFirstWithPhoneC(OrderCreditKd orderCreditKd,String type){
        //判断手机号是否为本平台
        OrderChannel order = orderChannelService.selectOrderChannelByNumberPhone(orderCreditKd.getPhoneNumber());
        if (order == null) {
            //如果不是流程结束
            setFlowOver(orderCreditKd,"手机号码不在本平台");
            return true;
        } else {
            //如果是 查询手机号和useid在平台是否结算过宽带订单
            OrderCreditKd orderCreditKdUsed = orderCreditKdService.selectOrderCriditKdByPhoneAndUserId(orderCreditKd.getPhoneNumber(), orderCreditKd.getUserId());
            if (orderCreditKdUsed != null) {
                //如果有流程结束
                setFlowOver(orderCreditKd,"手机号和userid在本平台计算过佣金");
                return true;
            } else {
                //如果没有 用手机号查询A 接口
                ActionResult selectCkResult = selectFirstMoneyCk(order);
                setFirstMoney(orderCreditKd, (OrderCreditCK) selectCkResult.getResultList());
                if(orderCreditKd.getFirstMoney() != null && orderCreditKd.getFirstMoney() >0){
                    //TODO 后加如果 0001 手机号没匹配规则，则需要重新查询接口，即发挥false 有时间优化
                    if(CommonConstant.RSP_CODE_0001.equals(type)){
                        if(orderCreditKd.getPhoneFirstMoney() == null){
                            return false;
                        }else{
                            if(!commissionRootRuleService.matchCommisionRuleKdC(orderCreditKd)){
                                return false;
                            }
                        }
                    }
                    setFlowNotOver(orderCreditKd);
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    private void setFirstMoney(OrderCreditKd orderCreditKd,FirstmoneyLog firstmoneyLog){
        orderCreditKd.setFirstMoney(firstmoneyLog.getFirstMoney());
        orderCreditKd.setFirstDate(firstmoneyLog.getFirstDate());
        orderCreditKd.setKdFirstMoney(firstmoneyLog.getFirstMoney());
        orderCreditKd.setUserId(firstmoneyLog.getUserId());
    }

    private void setSingleProduct(OrderCreditKd orderCreditKd){
        orderCreditKd.setProductType(CommonConstant.PRODUCT_TYPE_SINGLE);
    }

    private void setRelationProduct(OrderCreditKd orderCreditKd){
        orderCreditKd.setProductType(CommonConstant.PRODUCT_TYPE_RELATION);
    }

    private void setFirstMoney(OrderCreditKd orderCreditKd, OrderCreditCK orderCreditCk) {
        if (orderCreditCk != null) {
            if (orderCreditCk.getFirstMoney() != null) {
                orderCreditKd.setPhoneFirstMoney(orderCreditCk.getFirstMoney());

                if (orderCreditCk.getFirstMoney().compareTo(orderCreditKd.getKdFirstMoney() == null ? 0 : orderCreditKd.getKdFirstMoney()) > 0) {
                    orderCreditKd.setFirstMoney(orderCreditCk.getFirstMoney());
                    orderCreditKd.setFirstDate(orderCreditCk.getFirstDate());
                } else {
                    orderCreditKd.setFirstMoney(orderCreditKd.getKdFirstMoney());
                    orderCreditKd.setFirstDate(orderCreditKd.getFirstDate());
                }
            }
        }
    }





}
